package function;

import util.DBConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class RankingGenerator {
    public List<StatisticEntry> getTop50Visitors() {
        return getTop50("visitor_count");
    }

    public List<StatisticEntry> getTop50AddToCart() {
        return getTop50("add_to_cart_count");
    }

    public List<StatisticEntry> getTop50PaymentAmount() {
        return getTop50("payment_amount");
    }

    private List<StatisticEntry> getTop50(String column) {
        String sql = "SELECT id, " + column + " FROM statistics ORDER BY " + column + " DESC LIMIT 50";
        List<StatisticEntry> ranking = new ArrayList<>();
        try (Connection conn = DBConnectionUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                int id = rs.getInt("id");
                double value = rs.getDouble(column);
                ranking.add(new StatisticEntry(id, value));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ranking;
    }

    public static class StatisticEntry {
        private int id;
        private double value;

        public StatisticEntry(int id, double value) {
            this.id = id;
            this.value = value;
        }

        // Getters and Setters
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public double getValue() {
            return value;
        }

        public void setValue(double value) {
            this.value = value;
        }
    }
}